constint N = 1e5 + 5; classSolution { public: int cnt[N], L[N], R[N]; intminimumOperations(vector<int>& nums){ unordered_map<int, int> even; int n = nums.size(); for (int i = 0; i < n; i ++ ) { if ((i & 1) == 0) even[nums[i]] ++ ; else cnt[nums[i]] ++ ; } for (int i = 1; i < N; i ++ ) L[i] = max(L[i - 1], cnt[i]); for (int i = N - 2; i >= 1; i -- ) R[i] = max(R[i + 1], cnt[i]); int mx = 0; for (auto& [k, v] : even) { int cur = v + max(L[k - 1], R[k + 1]); mx = max(mx, cur); } return n - mx; } };
using LL = longlong; constint N = 1e5 + 5; classSolution { public: LL s[N], cnt[N]; longlongminimumRemoval(vector<int>& nums){ int n = nums.size(); for (int i = 0; i < n; i ++ ) { s[nums[i]] += nums[i]; cnt[nums[i]] += 1; } for (int i = 1; i < N; i ++ ) s[i] += s[i - 1]; for (int i = N - 3; i >= 1; i -- ) cnt[i] += cnt[i + 1]; LL ans = LLONG_MAX; for (int i = 0; i < n; i ++ ) { int cur = nums[i]; LL left = s[cur - 1]; LL right = (s[N - 1] - s[cur]) - 1ll * cnt[cur + 1] * cur; ans = min(ans, left + right); } return ans; } };
constint N = 20; int f[1 << N]; classSolution{ public: intmaximumANDSum(vector<int>& nums, int numSlots){ int n = nums.size(); memset(f, 0, sizeof(f)); int m = numSlots * 2; for (int i = 0; i < (1 << m); i ++ ) { int cnt = __builtin_popcount(i); if (i < 1or cnt > n) continue; for (int k = 0; k < numSlots; k ++ ) { if (i >> k & 1) f[i] = max(f[i], f[i - (1 << k)] + (nums[cnt - 1] & (k + 1))); int r = k + numSlots; if (i >> r & 1) f[i] = max(f[i], f[i - (1 << r)] + (nums[cnt - 1] & (k + 1))); } } int ans = 0; for (int i = 0; i < 1 << m ; i ++ ) if (__builtin_popcount(i) == n) ans = max(ans, f[i]); return ans; } };
constint N = 20; int f[1 << N][N]; classSolution{ public: intmaximumANDSum(vector<int>& nums, int numSlots){ int n = nums.size(); memset(f, 0, sizeof(f)); int m = numSlots * 2; for (int i = 0; i < (1 << m); i ++ ) { for (int j = 1; j <= n; j ++ ) { for (int k = 0; k < numSlots; k ++ ) { if (i >> k & 1) f[i][j] = max(f[i][j], f[i - (1 << k)][j - 1] + (nums[j - 1] & (k + 1))); int r = k + numSlots; if (i >> r & 1) f[i][j] = max(f[i][j], f[i - (1 << r)][j - 1] + (nums[j - 1] & (k + 1))); } } } int all = (1 << m) - 1; return f[all][n]; } };